package com.sky.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.sky.constant.MessageConstant;
import com.sky.constant.StatusConstant;
import com.sky.dto.SetmealDTO;
import com.sky.dto.SetmealPageQueryDTO;
import com.sky.entity.Setmeal;
import com.sky.entity.SetmealDish;
import com.sky.mapper.DishMapper;
import com.sky.mapper.DishSetmealMapper;
import com.sky.mapper.SetmealMapper;
import com.sky.result.PageResult;
import com.sky.result.Result;
import com.sky.service.SetmealService;
import com.sky.vo.DishItemVO;
import com.sky.vo.SetmealVO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;
import java.util.Set;

@Slf4j
@Service
public class SetmealServiceImpl implements SetmealService {
    @Autowired
    private SetmealMapper setmealMapper;
    @Autowired
    private DishSetmealMapper dishSetmealMapper;
    @Autowired
    private DishMapper dishMapper;
    @Autowired
    private RedisTemplate redisTemplate;
    @Override
    @Transactional
    public Result insertSetmeal(SetmealDTO setmealDTO) {
        //定义套餐对象
        Setmeal setmeal = new Setmeal();
        //将请求参数对应属性值放入套餐对象
        BeanUtils.copyProperties(setmealDTO, setmeal);
        //将套餐信息插入套餐表中
        setmealMapper.insert(setmeal);
        //获取请求参数中套餐菜品关联信息
        List<SetmealDish> setmealDishes = setmealDTO.getSetmealDishes();
        Long setmealId=setmealMapper.getSetmealId(setmeal.getName());
        //将套餐菜品关联信息插入套餐菜品关联表中
        for (SetmealDish setmealDish : setmealDishes) {
            setmealDish.setSetmealId(setmealId);
            dishSetmealMapper.insert(setmealDish);
        }
        return Result.success();
    }

    @Override
    public Result<PageResult> pageQuerySetmeal(SetmealPageQueryDTO setmealPageQueryDTO) {
        //获取请求参数中的状态、分类和套餐名称,并放入setmeal对象中
        Setmeal setmeal = new Setmeal();
        BeanUtils.copyProperties(setmealPageQueryDTO, setmeal);
        //获取分页信息
        Integer page = setmealPageQueryDTO.getPage();
        Integer pageSize = setmealPageQueryDTO.getPageSize();
        //开始分页
        PageHelper.startPage(page, pageSize);
        //调用mapper获取分页数据
        Page<SetmealVO> setmealVOPageData=setmealMapper.pageQuerySetmeal(setmeal);
        List<SetmealVO> setmealList = setmealVOPageData.getResult();
        List<SetmealVO> responseData=new ArrayList<>();
        //将套餐关联的菜品信息存入指定的响应数据中
        for (SetmealVO setmealVO : setmealList) {
            List<SetmealDish> setmealDishList=dishSetmealMapper.getDish(setmealVO.getId());
            setmealVO.setSetmealDishes(setmealDishList);
            responseData.add(setmealVO);
        }
        PageResult pageResult = new PageResult();
        pageResult.setTotal(setmealVOPageData.getTotal());
        pageResult.setRecords(responseData);
        return Result.success(pageResult);
    }

    @Override
    public Result updateSetmealStatus(Integer status, Long id) {
        Setmeal setmeal = new Setmeal();
        if (status==1){
            //调用mapper获取套餐内菜品是否有未起售的菜品，有则不能起售
            List<Long> dishIds=dishSetmealMapper.getDishId(id);
            for (Long dishId : dishIds) {
                //统计套餐内菜品为停售的数量
                if(dishMapper.getDishStatus(dishId)==0){
                    return Result.error(MessageConstant.SETMEAL_ENABLE_FAILED);
                }
            }
            //套餐菜品中没有停售的菜品可以起售套餐
            setmeal.setStatus(status);
            setmeal.setId(id);
            setmealMapper.update(setmeal);
            return Result.success();
        }else if(status==0){
            setmeal.setStatus(status);
            setmeal.setId(id);
            setmealMapper.update(setmeal);
            return Result.success();
        }else{
            return Result.error("状态只能为停售或者起售!");
        }

    }

    @Override
    @Transactional
    public Result delete(String ids) {
        String[] idList = ids.split(",");
        for (String sid : idList) {
            Long id = Long.valueOf(sid);
            //判断该套餐状态
            int setmealStatus=setmealMapper.getSetmealStatus(id);
            if(setmealStatus == StatusConstant.ENABLE){
                return Result.error(MessageConstant.SETMEAL_ON_SALE);
            }
            //删除套餐信息和套餐菜品关联信息
            setmealMapper.delete(id);
            dishSetmealMapper.delete(id);
            log.info("id为{}套餐删除成功",id);
        }
        return Result.success();
    }

    @Override
    public Result<SetmealVO> getSetmeal(Long id) {
        SetmealVO setmealVO=new SetmealVO();
        //根据套餐id获取套餐信息
        Setmeal setmeal=setmealMapper.getSetmeal(id);
        BeanUtils.copyProperties(setmeal,setmealVO);
        //根据套餐id获取套餐菜品关联信息
        List<SetmealDish> setmealDishes=dishSetmealMapper.getDish(id);
        setmealVO.setSetmealDishes(setmealDishes);
        return Result.success(setmealVO);
    }

    @Override
    public Result update(SetmealDTO setmealDTO) {
        //获取套餐请求数据
        Setmeal setmeal=new Setmeal();
        BeanUtils.copyProperties(setmealDTO,setmeal);
        int updateResult=setmealMapper.update(setmeal);
        //如果套餐更新成功，则更新套餐菜品关联信息
        if(updateResult>0){
            //将原套餐菜品关联信息全部删除
            dishSetmealMapper.delete(setmeal.getId());
            //从请求参数中获取套餐菜品关联信息
            List<SetmealDish> setmealDishes = setmealDTO.getSetmealDishes();
            //将新套餐菜品关联信息插入到数据库中
            for (SetmealDish setmealDish : setmealDishes) {
                setmealDish.setSetmealId(setmeal.getId());
                dishSetmealMapper.insert(setmealDish);
            }
            log.info("套餐和菜品关联信息添加成功!");
        }
        return Result.success();
    }
    /**
     * 条件查询
     * @param setmeal
     * @return
     */
    public List<Setmeal> list(Setmeal setmeal) {
        List<Setmeal> list = setmealMapper.list(setmeal);
        return list;
    }

    /**
     * 根据id查询菜品选项
     * @param id
     * @return
     */
    public List<DishItemVO> getDishItemById(Long id) {
        return setmealMapper.getDishItemBySetmealId(id);
    }

}
